|
|
|
|
|
|
|
The use of the navigation methods in the sample project is straightforward. The only part that merits further explanation is the DisplayCurrentRecord function. Notice that it checks for BOF or EOF conditions before displaying the field names and values for the current record. This step is necessary because attempting to access the Field object without a current record would cause an error. The working project is pictured in Figure 28.17. |
|
|
|
|
|
|
|
|
Figure 28.17
A sample program allows
the user to browse a recordset. |
|
|
|
|
|
|
|
|
Now that you know how to get information into a Recordset object and display it, you can take the next step and change (or update) the information in the database. If your recordset has been set up appropriately, you can change database information easily. Simply navigate to the appropriate record, assign a new value to each field you want to change, and finally call the Update method, as follows: |
|
|
|
|
|
|
|
|
rs.Fields("Author")="Simpson, Bart"
rs.Update |
|
|
|
|
|
|
|
|
|
In DAO, you call the Edit method before changing the value of a field. In ADO, changing the field's value automatically turns on edit mode; there is no Edit method. You can query the EditMode property to determine whether the current record is being edited.  |
|
|
|
|
|
|
|
|
|
|
Setting the LockType Property |
|
|
|
|
|
|
|
|
By default, ADO recordsets are read only. Attempting to change a field value in a read-only recordset causes an error. To add, change, or delete records in an ADO recordset, you must set the LockType property to a different value than the read-only default. For example, setting the CursorType and LockType properties as follows creates an updateable Access recordset: |
|
|
|
|
|
|
|
|
rs.CursorType = adOpenKeyset
rs.LockType = adLockOptimistic |
|
|
|
|
|
|
|
|
You might wonder why the LockType property is important. The answer is that if you are editing records in a multiuser database, you have to be concerned with record locking. Record locking means preventing other users from trying to edit the same database record at the same time. Record locking is controlled with the LockType property, which has the following values: |
|
|
|
|
|